<template>
  <div class="manage-container">
    <div class="header">
      <h1>文件管理</h1>
    </div>
    
    <div class="file-list">
      <el-table :data="fileList" style="width: 100%" v-loading="loading">
        <el-table-column prop="fileName" label="文件名" min-width="200">
          <template #default="scope">
            <el-tooltip :content="scope.row.fileName" placement="top" :show-after="1000">
              <span class="file-name">{{ scope.row.fileName }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="accessCode" label="访问码" width="120" align="center">
          <template #default="scope">
            <span class="access-code">{{ scope.row.accessCode }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="uploadTime" label="上传时间" width="180">
          <template #default="scope">
            {{ formatDate(scope.row.uploadTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" @click="copyShareLink(scope.row)">
              复制链接
            </el-button>
            <el-button type="danger" size="small" @click="deleteFile(scope.$index)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <div v-if="fileList.length === 0 && !loading" class="empty-data">
        <el-empty description="暂无上传文件" />
        <el-button type="primary" @click="goToUpload">立即上传</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const fileList = ref([])
const loading = ref(true)

// 获取文件列表
const getFileList = () => {
  loading.value = true
  // 从本地存储获取文件信息（实际项目中应该从后端获取）
  setTimeout(() => {
    fileList.value = JSON.parse(localStorage.getItem('cloudFiles') || '[]')
    loading.value = false
  }, 500)
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString()
}

// 复制分享链接
const copyShareLink = (file) => {
  const shareLink = `${window.location.origin}/cloud/download?code=${file.accessCode}`
  navigator.clipboard.writeText(shareLink)
    .then(() => ElMessage.success('分享链接已复制'))
    .catch(() => ElMessage.error('复制失败'))
}

// 删除文件
const deleteFile = (index) => {
  ElMessageBox.confirm(
    '确定要删除此文件吗？删除后将无法恢复。',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      // 从列表中删除
      const updatedList = [...fileList.value]
      updatedList.splice(index, 1)
      fileList.value = updatedList
      
      // 更新本地存储
      localStorage.setItem('cloudFiles', JSON.stringify(updatedList))
      
      ElMessage.success('文件已删除')
    })
    .catch(() => {
      // 取消删除
    })
}

// 跳转到上传页面
const goToUpload = () => {
  router.push('/cloud/upload')
}

onMounted(() => {
  getFileList()
})
</script>

<style scoped>
.manage-container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 2rem;
}

.header {
  margin-bottom: 2rem;
}

.header h1 {
  color: var(--el-color-primary);
}

.file-name {
  display: inline-block;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.access-code {
  font-family: monospace;
  font-weight: bold;
  letter-spacing: 2px;
}

.empty-data {
  margin-top: 3rem;
  text-align: center;
}

.empty-data .el-button {
  margin-top: 1rem;
}
</style>